package com.kbbywyclient.ui.activity;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.entity.ModifyOfRegistPassWord;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.RetrievePwdThreeModule;
import com.kbbywyclient.presenter.RetrievePwdThreePresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.RetrievePwdThreeContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.CheckFormat;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.widget.CustomToolBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:
 * Created by FHW
 * on 2017/3/31
 */

public class RetrievePwdThreeActivity extends BaseActivity implements RetrievePwdThreeContract.View {
    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;
    @BindView(R.id.et_pwd_new)
    EditText et_pwd_new;
    @BindView(R.id.et_pwd_again_new)
    EditText et_pwd_again_new;
    @BindView(R.id.tv_determine)
    TextView tv_determine;

    private String str_mobile = "";
    private String SalesID = "";

    private String str_pwd = "";
    private String str_pwd_again = "";

    private String str_code = "";

    @Inject
    RetrievePwdThreePresenter retrievePwdThreePresenter;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_retrieve_pwd_three;
    }

    @OnClick({R.id.tv_determine})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_determine:
                if (vertify()) {
                    retrievePwdThreePresenter.getModifyOfRegistPassWord(SalesID, str_mobile, str_pwd, "1", str_code, "");
                }
                break;
        }
    }

    /**
     * 验证密码
     *
     * @return
     */
    private boolean vertify() {
        if (str_pwd.isEmpty()) {
            ToastUtil.showShort("请输入新密码");
            return false;
        }
        if (!CheckFormat.isPwd(str_pwd)) {
            ToastUtil.showShort("密码须为6-15位");
            return false;
        }
        if (str_pwd_again.isEmpty()) {
            ToastUtil.showShort("请再次输入新密码");
            return false;
        }
        if (!str_pwd.equals(str_pwd_again)) {
            ToastUtil.showShort("密码和确认密码设置未统一");
            return false;
        }

        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("找回密码第三步");
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        v_toolbar.setLeftViewOnClickLinster(v -> finish());

        et_pwd_new.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                str_pwd = s.toString();
                isSubmitRegistFillIn();
            }
        });
        et_pwd_again_new.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                str_pwd_again = s.toString();
                isSubmitRegistFillIn();
            }
        });

    }

    /**
     * 判断密码填写完成的操作
     */
    private void isSubmitRegistFillIn() {
        if (CheckFormat.isPwd(str_pwd) && CheckFormat.isPwd(str_pwd_again)) {
            tv_determine.setBackgroundResource(R.drawable.textview_bg_14c095);
            if (CheckFormat.isPwdFinish(str_pwd) && CheckFormat.isPwdFinish(str_pwd_again)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive()) {
                    imm.hideSoftInputFromWindow(et_pwd_new.getWindowToken(), 0); //强制隐藏键盘
                }
            }
        } else {
            tv_determine.setBackgroundResource(R.drawable.textview_bg_dddddd);
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getRetrievePwdThreeComponent(new RetrievePwdThreeModule(this)).inject(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 第二步进来
     *
     * @param retrievePwdThree
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventMainThread(Event.RetrievePwdThree retrievePwdThree) {
        EventBus.getDefault().removeAllStickyEvents();

        str_mobile = retrievePwdThree.getMobile();
        SalesID = retrievePwdThree.getSalesID();
        str_code = retrievePwdThree.getStr_code();

    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucModifyOfRegist(ModifyOfRegistPassWord modifyOfRegistPassWord) {
        ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
    }

    @Override
    public void loadFaiModifyOfRegist(String errorMsg) {

    }
}
